define([], function() {
    var app = require("app");
    app.controller("originDataCtrl", function($scope, $q, $location) {
        // var userType=confirm("是否是超级用户?");
        var userType = true;
        var zutable2StoreBox = [undefined, undefined],
            zutable3StoreBox = [undefined, undefined]; //用于保存上一次url信息和filterid信息


        (function() {
            var startId = setInterval(function() {
                if (!checkNull($scope.zutable)) {
                    // zutableInit();
                    renderPageByUrlParams();
                    clearInterval(startId);
                }
            }, 1);
        })();

        function checkNull(obj) {
            if (obj == undefined || obj == null || obj == "" || obj.length <= 0)
                return true;
            if (typeof obj == "number" && isNaN(obj)) {
                return true;
            }
            return false;
        }

        //根据url参数来渲染界面,最多判断50次
        function renderPageByUrlParams() {
            console.log("根据url开始渲染表格");
            console.log("zutable 开始初始化");
            zutableInit();
            //获取url路径中的参数集合,格式：{key1:value1,...}
            var inParams = $location.search();

            //判断zutable是否已经有值

            //判断url中是否有模型id，modelid，再判断左侧模型列表中是否有对应的id
            var parent_view = Number(inParams['parent_view']);
            var parent_page = Number(inParams['parent_page']);
            var parent_rowId = Number(inParams['parent_rowId']);
            if (checkNull(parent_view)) {
                $location.search("parent_view", 1);
                parent_view = 1;
            }

            if (checkNull(parent_page) || parent_page == -1) {
                parent_page = 1;
                $location.search("parent_page", 1);
            }

            if (checkNull(parent_rowId) || parent_rowId == -1) {
                parent_rowId = -1;
                $location.search("parent_rowId", -1);
            }

            $scope.$apply(function() {
                $scope.zutable.afterTableRenderOk(function() {
                    console.log("zutable 刷新完成 开始子视图切换");
                    var rowObj = $("zutable tr[data-rowid='rowid" + parent_rowId + "']:eq(0)");
                    console.log("rowObj length=" + rowObj.length);
                    if (rowObj.length <= 0) {
                        // clearInterval(id);
                        return;
                    }
                    var rowData = angular.element(rowObj).inheritedData().$ngModelController.$modelValue;

                    var sub_page = Number(inParams['sub_page']);
                    var sub_view = Number(inParams['sub_view']);
                    var sub_rowId = Number(inParams['sub_rowId']);

                    var zutables = [$scope.zutable, $scope.zutable2, $scope.zutable3];
                    var nowTable = undefined;
                    if (!checkNull(sub_view) && sub_view != -1) {
                        nowTable = zutables[sub_view - 1];

                        var param = {};
                        param['sub_page'] = sub_page;
                        param['sub_rowId'] = sub_rowId;

                        if (sub_view == 2) {
                            console.log("zutable2 开始初始化");
                            checkModelEvent(rowData, function(param) {
                                $scope.$apply(function() {
                                    console.log("zutable2 开始刷新");
                                    if (checkNull(param['sub_page'] || param['sub_page'] == -1)) {
                                        param['sub_page'] = 1;
                                    }

                                    if (checkNull(param['sub_rowId']) || param['sub_rowId'] == -1) {
                                        param['sub_rowId'] = -1;
                                    }

                                    $scope.zutable2.notifyTableToRefresh(param['sub_page'], param['sub_rowId']);
                                    $location.search("sub_page", param['sub_page']);
                                    $location.search("sub_rowId", param['sub_rowId']);
                                })
                            }, param);
                        }

                        if (sub_view == 3) {
                            checkDataEvent(rowData, function(param) {
                                $scope.$apply(function() {
                                    console.log("zutable3 开始刷新");
                                    if (checkNull(param['sub_page'] || param['sub_page'] == -1)) {
                                        param['sub_page'] = 1;
                                    }

                                    if (checkNull(param['sub_rowId']) || param['sub_rowId'] == -1) {
                                        param['sub_rowId'] = -1;
                                    }

                                    $scope.zutable3.notifyTableToRefresh(param['sub_page'], param['sub_rowId']);
                                    $location.search("sub_page", param['sub_page']);
                                    $location.search("sub_rowId", param['sub_rowId']);
                                })
                            }, param);
                        }

                    }
                });
                $scope.zutable.notifyTableToRefresh(parent_page, parent_rowId);
            })
        }

        function zutableInit() {
            $scope.$apply(function() {
                $scope.filterid = "100";
                $scope.baseurl = "http://localhost:3000/api/rest/v3/workspaces/100/models/"; //表格获取数据的基本地址对象，双向绑定
                console.log("请求地址改变");
                $scope.switchDiv = true;
                $scope.zutable.defineTableName("原始数据");
                $scope.zutable.showRowOperationButton(false);

                $scope.zutable.afterPageButtonClick(function() {
                    $location.search("parent_page", $scope.zutable.getNowPage());
                });
                $scope.zutable.afterTableItemClick(function() {
                    $location.search("parent_page", $scope.zutable.getNowPage());
                    $location.search("parent_rowId", $scope.zutable.getNowSelectItem());
                });
                $scope.zutable.addFreeColumn({
                    show: true,
                    name: "查看",
                    style: "width:7%",
                    content: '<span title="查看模型" class="mif-medium myCursor" ng-click="checkModel(td)"></span>' + '&nbsp' + '<span title="查看数据" class="mif-table myCursor" ng-click="checkData(td)"></span>',
                    event: {
                        checkModel: function(row) {
                            $location.search("parent_page", $scope.zutable.getNowPage());
                            $scope.zutable.selectItem(row.rowId);
                            $location.search("parent_rowId", row.rowId);
                            checkModelEvent(row, angular.noop, null);
                        },
                        checkData: function(row) {
                            $location.search("parent_page", $scope.zutable.getNowPage());
                            $scope.zutable.selectItem(row.rowId);
                            $location.search("parent_rowId", row.rowId);
                            checkDataEvent(row, angular.noop, null);
                        },
                    }
                });
                // console.log("zutable 开始刷新");
                // $scope.zutable.notifyTableToRefresh();
            })
        }

        function hideElement(className, func, arg, direction) {
            var obj = $("div[class*='" + className + "']:eq(0)");

            var styleAnimation = {};
            var speed = 100;
            if (direction == 'left') {
                var left = obj.css("left").split("px")[0];
                styleAnimation.left = Number(left) - Number(10 * 20) + "px";
                styleAnimation.opacity = 0;
            }
            if (direction == 'right') {
                var left = obj.css("left").split("px")[0];
                styleAnimation.left = Number(left) + Number(10 * 20) + "px";
                styleAnimation.opacity = 0;
            }

            obj.animate(styleAnimation, speed, function() {
                console.log(className + "hide ok");
                obj.css("width", "0%");
                func(arg);
            });
        }

        function showElement(className, func, arg, direction) {
            var view = className.split("model")[1];
            if (view != 1) {
                $location.search("sub_view", view);
            }

            if (view == 1) {
                $location.search("sub_view", -1);
                $location.search("sub_page", -1);
                $location.search("sub_rowId", -1);
            }

            var obj = $("div[class*='" + className + "']:eq(0)");
            obj.css("opacity", 0);
            obj.css("width", "100%");

            var styleAnimation = {};
            var speed = 100;
            if (direction == 'left') {
                var left = obj.css("left").split("px")[0];
                styleAnimation.left = Number(left) - Number(10 * 20) + "px";
                styleAnimation.opacity = 1;
            }
            if (direction == 'right') {
                var left = obj.css("left").split("px")[0];
                styleAnimation.left = Number(left) + Number(10 * 20) + "px";
                styleAnimation.opacity = 1;
            }

            obj.animate(styleAnimation, speed, function() {
                console.log(className + "show ok");
                func(arg);
            });
        }

        /*
            rowInformation:行对象
            func:执行完后的回调函数
            arg:func的参数
        */
        function checkModelEvent(rowInformation, func, arg) {
            hideElement("model1", function(rowInformation) {
                $scope.$apply(function() {
                    $scope.model1Show = false;
                    $scope.model2Show = true;
                    zutable2Init(rowInformation, func, arg);
                });
            }, rowInformation, "right");
        }

        function checkDataEvent(rowInformation, func, arg) {
            hideElement("model1", function(rowInformation) {
                $scope.$apply(function() {
                    $scope.model1Show = false;
                    $scope.model3Show = true;
                    zutable3Init(rowInformation, func, arg);
                });
            }, rowInformation, "right");
        }

        function zutable3Init(rowInformation, func, arg) {
            console.log("传给 zutable3 的信息");
            console.log(rowInformation);
            $scope.baseurl3 = "http://localhost:3000/api/rest/v3/workspaces/100/models/" + rowInformation.rowId + "/"; //表格获取数据的基本地址对象，双向绑定
            $scope.filterid3 = "100";
            $scope.moduleName = rowInformation["c_10001"];

            var shouldUpdateTable = (function() {
                var flag = true;

                if (_.contains(zutable3StoreBox, $scope.baseurl3) && _.contains(zutable3StoreBox, $scope.filterid3)) {
                    flag = false;
                } else {
                    flag = true;
                }
                zutable3StoreBox[0] = $scope.baseurl3;
                zutable3StoreBox[1] = $scope.filterid3;
                return flag;
            })();

            if (!checkNull($scope.zutable3)) {
                $scope.zutable3.defineTableName("数据信息");
                $scope.zutable3.showRowOperationButton(userType);
                $location.search("sub_page", $scope.zutable3.getNowPage());
                $location.search("sub_rowId", $scope.zutable3.getNowSelectItem());
                $scope.zutable3.afterPageButtonClick(function() {
                    $location.search("sub_page", $scope.zutable3.getNowPage());
                });
                $scope.zutable3.afterTableItemClick(function() {
                    $location.search("sub_page", $scope.zutable3.getNowPage());
                    $location.search("sub_rowId", $scope.zutable3.getNowSelectItem());
                });

                //此处的判断，是用来确定，是renderParam函数在操作此函数，还是原始的点击事件在操作此函数
                if (func == angular.noop && shouldUpdateTable) {
                    $scope.zutable3.notifyTableToRefresh();
                }
                showElement("model3", func, arg, "right");
            }
        }

        function zutable2Init(rowInformation, func, arg) {
            console.log("传给 zutable2 的信息");
            console.log(rowInformation);
            $scope.baseurl2 = "http://localhost:3000/api/rest/v3/workspaces/100/models/" + rowInformation.rowId + "/"; //表格获取数据的基本地址对象，双向绑定
            $scope.filterid2 = "100";
            $scope.moduleName = rowInformation["c_10001"];

            var shouldUpdateTable = (function() {
                var flag = true;

                if (_.contains(zutable2StoreBox, $scope.baseurl2) && _.contains(zutable2StoreBox, $scope.filterid2)) {
                    flag = false;
                } else {
                    flag = true;
                }
                zutable2StoreBox[0] = $scope.baseurl2;
                zutable2StoreBox[1] = $scope.filterid2;
                return flag;
            })();

            if (!checkNull($scope.zutable2)) {
                $scope.zutable2.defineTableName("模型信息");
                $scope.zutable2.showRowOperationButton(userType);
                $location.search("sub_page", $scope.zutable2.getNowPage());
                $location.search("sub_rowId", $scope.zutable2.getNowSelectItem());
                $scope.zutable2.afterPageButtonClick(function() {
                    $location.search("sub_page", $scope.zutable2.getNowPage());
                });
                $scope.zutable2.afterTableItemClick(function() {
                    $location.search("sub_page", $scope.zutable2.getNowPage());
                    $location.search("sub_rowId", $scope.zutable2.getNowSelectItem());
                });

                //此处的判断，是用来确定，是renderParam函数在操作此函数，还是原始的点击事件在操作此函数
                if (func == angular.noop && shouldUpdateTable) {
                    $scope.zutable2.notifyTableToRefresh();
                }
                showElement("model2", func, arg, "right");
            }
        }

        $scope.model2Back = function() {
            hideElement("model2", function() {
                $scope.$apply(function() {
                    $scope.model1Show = true;
                    $scope.model2Show = false;
                    showElement("model1", angular.noop, null, "left");
                })
            }, null, "left");
        }

        $scope.model3Back = function() {
            hideElement("model3", function() {
                $scope.$apply(function() {
                    $scope.model1Show = true;
                    $scope.model3Show = false;
                    showElement("model1", angular.noop, null, "left");
                })
            }, null, "left");
        }
    });
});
